import java.sql.*;
import java.text.MessageFormat;
import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://127.0.0.1:3306/java113";
            String username = "root";
            String password = "gjm041027";

            connection = DriverManager.getConnection(url, username, password);

            statement = connection.createStatement();

            String sql = "select student_id, sn, name, mail, class_id from student;";
            resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                Long student_id = (Long)resultSet.getLong("student_id");
                String sn = resultSet.getString("sn");
                String name = resultSet.getString("name");
                String mail = resultSet.getString("mail");
                Long class_id = (Long)resultSet.getLong("class_id");
                System.out.println(MessageFormat.format("student_id = {0}, sn = {1}, name = {2}, mail = {3}, class_id = {4}",
                        student_id, sn, name, mail, class_id));
            }

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
